//
//  WaterfallViewController.swift
//  elarc01
//
//  Created by elarc on 16/6/16.
//  Copyright © 2016年 elarc. All rights reserved.
//

import UIKit

class WaterfallViewController: UIViewController{

    var waterCollectionView : UICollectionView?
    var dataSource  = NSMutableArray()
    var controlAnim = NSMutableArray()
    var cellItemWidth = Float(0.0)
    var headerActivityView : DTIActivityIndicatorView?
    var footerActivityView : DTIActivityIndicatorView?
    var page = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        buildCollectionView()
        let tempArray = ["01.jpg","02.jpg","03.jpg","04.jpg","05.jpg","06.jpg","07.jpg","08.jpg","09.jpg","010.jpg"]
        dataSource.addObjectsFromArray(tempArray)
        let tempAni = NSMutableArray()
        for _ in 0..<dataSource.count{
            tempAni.addObject("false")
        }
        controlAnim = tempAni
        // Do any additional setup after loading the view.
    }
    /// MARK: - Build buildCollectionView
    private func buildCollectionView() {
        waterCollectionView = UICollectionView(frame: ScreenBounds, collectionViewLayout: WaterLayout())
        waterCollectionView!.backgroundColor = UIColor.whiteColor()
        waterCollectionView?.delegate = self
        waterCollectionView?.dataSource = self
        waterCollectionView?.registerClass(WaterfallCell.self, forCellWithReuseIdentifier: "WaterCell")
        view.addSubview(waterCollectionView!)
        confirDTIActivityView()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension WaterfallViewController:UICollectionViewDelegate,UICollectionViewDataSource,WaterfallProtocol{
    //UICollectionViewDataSource 代理
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return dataSource.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("WaterCell", forIndexPath: indexPath) as! WaterfallCell
        //重新调整图片的大小  按比例调整到宽度为cellItemWidth的情况
        cell.imageView!.image = UIImage(named: dataSource[indexPath.row] as! String)!
        if controlAnim[indexPath.row] as! String == "false"{
            displayCellAnimation(cell.contentView)
            controlAnim[indexPath.row] = "true"
        }
        
        
        return cell
    }
    func displayCellAnimation(view:UIView){
        view.transform = CGAffineTransformMakeScale(0.0, 0.0)
        UIView.animateWithDuration(0.3, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 7, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            view.transform = CGAffineTransformIdentity
            
        }) { (success) -> Void in
            
        }
    }
    //waterfallProtocol询问cell高度的代理
    func collectionView_heightForItemAtIndexPath(view: UICollectionView, layout: WaterLayout, path: NSIndexPath) -> Float {
        //重新调整图片的大小  按比例调整到宽度为cellItemWidth的情况
        let origiImage = UIImage(named: dataSource[path.row] as! String)!
        let origiImageWidth = UIImage(named: dataSource[path.row] as! String)!.size.width
        
        let cellItemHeight = origiImage.size.height * (CGFloat(cellItemWidth - 20 )/origiImageWidth)
        return Float(cellItemHeight + CGFloat(58))
        
        
    }
    func waterfall_itemWidth(itemWidth: Float) {
        cellItemWidth = itemWidth
    }

    func confirDTIActivityView(){
        //header
        headerActivityView = DTIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
        self.waterCollectionView!.addSubview(headerActivityView!)
        headerActivityView!.translatesAutoresizingMaskIntoConstraints = false
        headerActivityView!.indicatorColor = UIColor.lightGrayColor();
        headerActivityView!.indicatorStyle = "wp8";
        headerActivityView!.backgroundColor = UIColor.clearColor();
    }
}
